﻿using Nai.Communication.VarProxy;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FanucVarProxy.ClientUserControl
{
    public partial class FormSysVarValue : Form
    {
        public FormSysVarValue()
        {
            InitializeComponent();
        }

        private void FormNumericRegValue_Load(object sender, EventArgs e)
        {
            this.comboBox_types.Items.Add(SysVarValueType.Boolean);
            this.comboBox_types.Items.Add(SysVarValueType.Integer);
            this.comboBox_types.Items.Add(SysVarValueType.Float);
            this.comboBox_types.Items.Add(SysVarValueType.String);

            this.comboBox_types.SelectedIndex = 0;
        }

        public Type VarValueType { get; private set; }
        public object VarValue { get; private set; }

        private void button_set_Click(object sender, EventArgs e)
        {
            string selectedType = this.comboBox_types.SelectedItem.ToString();
            string valueStr = this.textBox_value.Text.Trim().ToUpper();
            if (selectedType == SysVarValueType.Boolean.ToString())
            {
                this.VarValueType = typeof(bool);
                this.VarValue = valueStr == "TRUE" || valueStr == "ON" ? true : false;
            }
            if (selectedType == SysVarValueType.Integer.ToString())
            {
                this.VarValueType = typeof(int);
                if (int.TryParse(valueStr, out int intValue))
                {
                    this.VarValue = intValue;
                }
                else
                {
                    this.VarValue = null;
                }
            }
            if (selectedType == SysVarValueType.Float.ToString())
            {
                this.VarValueType = typeof(float);
                if (float.TryParse(valueStr, out float floatValue))
                {
                    this.VarValue = floatValue;
                }
                else
                { 
                    this.VarValue = null;
                }
            }
            if (selectedType == SysVarValueType.String.ToString())
            {
                this.VarValueType = typeof(string);
                this.VarValue = valueStr;
            }

            this.Close();
        }
    }
}
